package com.xcx.baseManger.controller;


import com.xcx.baseManger.service.AclUserService;
import com.xcx.commoncore.request.aclrequest.userrequest.GetUserByOpenidRequest;
import com.xcx.commoncore.request.aclrequest.userrequest.UpdateUseChooseRequest;
import com.xcx.commoncore.request.aclrequest.userrequest.UpdateUseDetailsRequest;
import com.xcx.commoncore.request.aclrequest.userrequest.UpdateUseRequest;
import com.xcx.commoncore.request.logrequest.LogRequest;
import com.xcx.commoncore.request.logrequest.LoginMobileRequest;
import com.xcx.commoncore.request.logrequest.RegisterMobileRequest;
import com.xcx.commoncore.request.logrequest.RegisterRequest;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-10-22
 */


@Api(tags = "用户管理api")
@RestController
@RequestMapping("/baseManger/aclUser")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class AclUserController {


    final private AclUserService aclUserService;

    //注册 StringUtils.isNotBlank(req.getCheckDetailName())
//    @PostMapping("/register")
//    @ApiOperation(value = "注册接口")
//    public ReturnMessage<Object> registerUser(@RequestBody @Valid RegisterRequest registerVo, BindingResult br) {
//
//        log.info("Request:{}", registerVo);
//        if (registerVo == null) {
//            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
//        }
//        if (br.hasErrors()) {
//            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
//        }
//
//        return aclUserService.register(registerVo);
//    }

    @PostMapping("/registerMobile")
    @ApiOperation(value = "小程序注册接口")
    public ReturnMessage<Object> registerMobile(@RequestBody @Valid RegisterMobileRequest registerVo, BindingResult br) {

        log.info("Request:{}", registerVo);
        if (registerVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        return aclUserService.registerMobile(registerVo);
    }






//    @ApiOperation(value = "登陆接口")
//    @PostMapping("/login")
//    public ReturnMessage<Object> login(@RequestBody @Valid LogRequest LoginVo, BindingResult br) {
//        log.info("Request:{}", LoginVo);
//        if (LoginVo == null) {
//            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
//        }
//        if (br.hasErrors()) {
//            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
//        }
//        return aclUserService.login(LoginVo);
//    }


    @ApiOperation(value = "小程序登陆接口")
    @PostMapping("/loginWx")
    public ReturnMessage<Object> loginWx(@RequestBody @Valid LoginMobileRequest LoginVo, BindingResult br) {
        log.info("Request:{}", LoginVo);
        if (LoginVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return aclUserService.loginWx(LoginVo);
    }





    @ApiOperation(value = "根据用户openid查询用户信息")
    @PostMapping("/getByOpenid")
    public ReturnMessage<Object> getByOpenid(@RequestBody @Valid GetUserByOpenidRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return aclUserService.getByOpenid(request);
    }


    @ApiOperation(value = "修改用户基本信息")
    @PostMapping("/updateBaseInfo")
    public ReturnMessage<Object> updateBaseInfo(@RequestBody @Valid UpdateUseRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return aclUserService.updateBaseInfo(request);
    }

    @ApiOperation(value = "修改用户详细信息")
    @PostMapping("/updateDetailInfo")
    public ReturnMessage<Object> updateDetailInfo(@RequestBody @Valid UpdateUseDetailsRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return aclUserService.updateDetailInfo(request);
    }



    @ApiOperation(value = "修改用户择偶信息")
    @PostMapping("/updateChooseInfo")
    public ReturnMessage<Object> updateChooseInfo(@RequestBody @Valid UpdateUseChooseRequest request, BindingResult br) {
        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return aclUserService.updateChooseInfo(request);
    }

    @GetMapping("/updateUserFileIndex")
    public ReturnMessage<Object> updateUserFileIndex(@RequestParam("username") String username, @RequestParam("filePath")String filePath) {
        log.info("Request:" + username +filePath);
        return aclUserService.updateUserFileIndex(username,filePath);


    }


    @ApiOperation(value = "删除用户信息")
    @DeleteMapping("/removeByMobile")
    public ReturnMessage<Object> removeByMobile(@RequestParam("mobile") String mobile) {

        return aclUserService.removeByMobile(mobile);


    }


































}

